package com.tools.cleanmaster.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.model.item.ImageTrashBucket;
import com.tools.cleanmaster.model.item.TrashItem;

import java.io.File;
import java.util.List;

/**
 * Created by lianzhan on 15/12/22.
 */
public class ImageAdapter extends DeepAdapter {
    /**
     * 图片集列表
     */
    private List<TrashItem> mDataList;

    public ImageAdapter(Context context, List<TrashItem> dataList) {
        super(context);
        this.mDataList = dataList;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = View.inflate(mContext, R.layout.item_image_bucket, null);
            holder.iv = convertView.findViewById(R.id.image);
            holder.name = convertView.findViewById(R.id.name);
            holder.count = convertView.findViewById(R.id.count);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        ImageTrashBucket item = (ImageTrashBucket) mDataList.get(position);

        holder.count.setText("" + item.count);
        holder.name.setText(item.bucketName);
        String path = item.imageList.get(0).imagePath;

        if (!TextUtils.isEmpty(path)) {
            Glide.with(mContext).load(new File(path)).centerCrop().into(holder.iv);
        }

        return convertView;
    }

    @Override
    protected int getDefaultImageId() {
        return R.drawable.image_default;
    }

    private static class Holder {
        private ImageView iv;

        private TextView name;

        private TextView count;
    }

    public void setDatas(List<TrashItem> items) {
        mDataList = items;
        notifyDataSetChanged();
    }
}
